package service

import (
	"encoding/json"
	"gameserver/gcommon"
	"gameserver/server"
	"gameserver/server/Login/internal"

	"gitee.com/night-tc/gobige/dblink"
	"gitee.com/night-tc/gobige/etcd"
	"gitee.com/night-tc/gobige/global"
	"gitee.com/night-tc/gobige/logger"
	"gitee.com/night-tc/gobige/serverMgr"
	. "gitee.com/night-tc/gobige/serverMgr"
	// . "gitee.com/night-tc/gobige/service"
)

// 根据类型生成一个新的IServerInfo对象，用与从etcd读出来后进行填充
func (this *LoginService) NewInfo(stype global.ServerTypeEnum) IServerInfo {
	return new(ServerInfo)
}

// 获取自己服务器需要注册的信息
func (this *LoginService) GetSelfInfo() IServerInfo {
	//不用注册
	return nil
}

// 获取到新的服务器信息的时候回调，一般用来判断是否需要连接这个服务器
func (this *LoginService) WatchInfoCallBack(sinfo IServerInfo) {
	/*
	   如果发现是Linker服，需要创建好对应的连接信息
	   然后放在管理器中

	*/
	data, _ := json.Marshal(sinfo)
	logger.Debugf(logger.LogKey_Etcd, "ServerInfo:%s", string(data))
	if sinfo.GetSrvType() == server.ServerType_Linker {
		linkermd := this.LinkerMgr.GetLinkerSrv(sinfo.GetGroupID())
		linkermd.AddLinkerClient(sinfo)
	}
}

// 获取ETCD的实例
func (this *LoginService) GetEtcd() *etcd.EtcdAccess {
	return this.Etcd
}

// 获取服务器负载器
func (this *LoginService) GetDirMgr() *serverMgr.DiscoveryMgr {
	return this.DisMgr
}

// 获取mysql管理器
func (this *LoginService) GetMysqlMgr() *dblink.MysqlAccess {
	return this.MysqlMgr
}

// 获取本服务器实例配置
func (this *LoginService) GetSInfo() *gcommon.ServiceConfig {
	return this.Sinfo
}

// 获取Linker服务实例
func (this *LoginService) GetLinkerSrv(groupid uint32) *internal.LinkerSrv {
	return this.LinkerMgr.GetLinkerSrv(groupid)
}
